﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SDFL.KeyPoints.DesignPattern.Singleton.Structural
{
    public class ThreadUnsafeSingleton
    {
        private static ThreadUnsafeSingleton _instance;
        private ThreadUnsafeSingleton()
        {
            // To display non thread safe, 
            // assume that the constructor will consume long time            
            Thread.Sleep(1000 * 20);
        }

        public static ThreadUnsafeSingleton Instance
        {
            get
            {
                if (_instance == null) // 是为了 "lazy Initialization" , 程序启动时，不用的类就不会被new实例出来
                {
                    Console.WriteLine("One new singleton object is initiated"); // You will see, more singleton objects are created
                    _instance = new ThreadUnsafeSingleton();
                }
                return _instance;
            }
        }
    }
}
